//
//  JKStatusViewModel.swift
//  JKWeibo
//
//  Created by albert on 16/5/17.
//  Copyright © 2016年 albert. All rights reserved.
//  MVVM

import UIKit

class JKStatusViewModel: NSObject {
    /// 模型对象
    var status: JKStatusModel
    
    init(status: JKStatusModel) {
        self.status = status
        
        // 1.处理会员图标
        if let rank = status.user?.mbrank {
            mbrankImage = nil
            if rank >= 1 && rank <= 6 {
                mbrankImage = UIImage(named: "common_icon_membership_level\(rank)")
            }
        }
        
        // 2.处理认证图标
        var name = ""
        switch status.user?.verified_type ?? -1
        {
        case 0:
            name = "avatar_vip"
        case 2, 3, 5:
            name = "avatar_enterprise_vip"
        case 220:
            name = "avatar_grassroot"
        default:
            name = ""
        }
        if name != "" {
            verified_image = UIImage(named: name)
            
        }else { // 注意解决重用问题
            verified_image = nil
        }
        
        // 3.处理时间
        if let timeStr = status.created_at where timeStr != ""
        {
            // "Sun Dec 06 11:10:41 +0800 2015"
            // 1.将服务器返回的时间格式化为NSDate
            let createDate = NSDate.createDate(timeStr, formatterStr: "EE MM dd HH:mm:ss Z yyyy") //formatter.dateFromString(timeStr)!
            
            // 2.设置微博发布时间对应的字符串
            created_timeStr = createDate.descriptionStr()
        }
        
        // 4.处理来源
        // <a href="http://app.weibo.com/t/feed/310OQS" rel="nofollow">精彩微客</a>
        if let sourceStr: NSString = status.source where sourceStr != ""
        {
            // range默认从前面开始寻找到第一个就结束
            let startIndex = sourceStr.rangeOfString(">").location + 1
            
            // 获取需要截取的长度，下面两个方法都可以
            //let length = sourceStr.rangeOfString("</").location - startIndex
            // 从后面开始查询BackwardsSearch
            let length = sourceStr.rangeOfString("<", options: NSStringCompareOptions.BackwardsSearch).location - startIndex
            
            let str = sourceStr.substringWithRange(NSMakeRange(startIndex, length))
            
            source_str = "来自" + str
        }
        
        // 5.处理配图URL
        // 1.从模型中取出配图数组
        //if let picUrls = status.pic_urls {
        // 当有转发的时候，就处理转发配图，没有转发就处理原创配图
        if let picUrls = (status.retweeted_status != nil) ? status.retweeted_status?.pic_urls : status.pic_urls {
            
            thumbnail_pic_urls = [NSURL]()
            bmiddlePicUrls = [NSURL]()
            
            // 2.遍历配图数组
            for dict in picUrls {
                // 2.1取出图片的URL字符串
                guard var urlStr = dict["thumbnail_pic"] as? String else {
                    continue
                }
                // 2.2根据字符串创建URL
                let url = NSURL(string: urlStr)!
                thumbnail_pic_urls?.append(url)
                
                // 2.3处理大图
                urlStr = urlStr.stringByReplacingOccurrencesOfString("thumbnail", withString: "bmiddle")
                bmiddlePicUrls?.append(NSURL(string: urlStr)!)
            }
        }
        
        // 6.处理转发
        if let text = status.retweeted_status?.text {
            let name = status.retweeted_status?.user?.screen_name ?? ""
            forwardText = "@" + name + ": " + text
        }
    }

    /** 处理好的微博创建时间字符串 */
    var created_timeStr: String?
    /** 处理好的微博来源 */
    var source_str: String?
    /** 微博认证图片  */
    var verified_image: UIImage?
    /** 会员图片 */
    var mbrankImage: UIImage?
    /** 所有配图的URL */
    var thumbnail_pic_urls: [NSURL]?
    /** 转发微博格式化后正文 */
    var forwardText: String?
    /** 所有配图 大图的URL */
    var bmiddlePicUrls: [NSURL]?
}
